<?php
class Cms_model extends CI_Model{
	
	//var $CI;
	
	function Cms_model(){
		parent::__construct();
		
		//$CI =& get_instance();
		//$this->load->library('encrypt');
		
		$this->load->helper('text');
	}
	
	function Login($userid, $pwd){
		$result = false;		
		$userid = strtolower($userid);
		$sql = "SELECT * FROM qf_user WHERE uid =".$this->db->escape($userid)." AND disabled = 0"; 		
		//log_message('debug', 'test##'.$sql);
		
		//$query = $this->db->query($sql, array($userid)); 
		$query = $this->db->query($sql); 
		foreach ($query->result() as $row)
		{
			//$this->encrypt->encode($pwd);
			$pwdstr = $row->pwd;
			if(!empty($pwdstr)){	
				$pwdstr = $this->encrypt->decode($pwdstr);		
				//$pwdstr = $this->encrypt->decode($pwdstr);
				if(strcasecmp($pwdstr, $pwd) == 0){
					$result = true;
					break;
				}
			}
		}
		return $result;		
	}
	
	function ExistUserID($userid){		
		$sql = "SELECT * FROM qf_user WHERE uid ='$userid'";
		$query = $this->db->query($sql); 
		if ($query->num_rows()> 0)
			return true;
		else
			return false;		
	}
	
	function hasChildColumnsBySiteid($siteid)
	{
		$sql = "SELECT count(*) as num FROM qf_column where disabled=0 and sid='$siteid' "; 
		$query = $this->db->query($sql); 
		if ($query->num_rows() > 0){
			$row = $query->row(); 
			if( $row->num == 0 )
				return false;
			else
				return true;
		}
	}
	
	function hasChildColumns($cid)
	{
		$sql = "SELECT count(*) as num FROM qf_column where disabled=0 and parentid='$cid' "; 
		$query = $this->db->query($sql); 
		if ($query->num_rows() > 0){
			$row = $query->row(); 
			if( $row->num == 0 )
				return false;
			else
				return true;
		}
	}
	
	function GetJsonData($parentID)
	{
		/** res = '[' 
			res += '{ "title": "Node 1", "key": "k1", "isLazy": true },' 
			res += '{ "title": "Node 2", "key": "k2", "isLazy": true },' 
			res += '{ "title": "Node 3", "key": "k3", "isLazy": true }' # no trailing "," at the last line
			res += ']'
		*/
		
		try {		
			$col="";
			$result="";
			if($parentID == "")
			{
				//site auth filter
				$uid = $_SESSION['qf_userid']; //$this->session->userdata('qf_userid');
				$usersites = $this->GetUserSites($uid);
				if($uid != 'admin')
					if(!is_array($usersites) )
						echo "";
				
				$siteString = '';
				
				$sql = "SELECT * FROM qf_site where disabled=0 order by sortno,sname ";
				$query = $this->db->query($sql); 
				foreach ($query->result() as $row)
				{
					//filter
					if($uid != 'admin'){
						//$sid = $row->sid;
						if( !in_array($row->sid, $usersites))
							continue;
					}
					
					//data
					$siteString .= ",{ \"title\": \"".$row->sname."\"";
					$siteString .= ",\"key\":\"".$row->sid."\"";
					$siteString .= ",\"url\":\"/cms/columns/".$row->sid."\"";
					$siteString .= ",\"isFolder\":\"true\" ";
					$siteString .= ",\"icon\":\"server.png\" ";
					
					if($this->hasChildColumnsBySiteid($row->sid))
						$siteString .=",\"isLazy\": \"true\" ";
						
					$siteString .= "}";
				}
				if(!empty($siteString))
					$siteString = "[".substr($siteString,1)."]";
					
				$result .= "[{\"title\": \"常规\", \"key\": \"setting\", \"isFolder\": true, \"expand\":true, \"children\": [";
				//$result .= "{\"title\": \"我的文档\", \"key\": \"chgpwd\", \"url\": \"/cms/users\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"mydoc.png\" },";
				//$result .= "{\"title\": \"文档审核\", \"key\": \"chgpwd\", \"url\": \"/cms/roles\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"checkdoc.png\" },";
				$result .= "{\"title\": \"文档查询\", \"key\": \"chgpwd\", \"url\": \"/cms/searchart\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"search.png\"  },";
				$result .= "{\"title\": \"修改密码\", \"key\": \"chgpwd\", \"url\": \"/cms/changepwd\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"lock.png\"  }";
				$result .= "]}";
				
				$result .= ",{ \"title\": \"网站管理\", \"key\": \"f_site\", \"url\": \"/cms/sites\", \"isFolder\": true, \"isLazy\": true , \"expand\":true ";
				if(!empty($siteString))
					$result .= ",\"children\": ".$siteString ;
				$result .="}";
				$result .= ",{\"title\": \"系统设置\", \"key\": \"setting\", \"isFolder\": true, \"expand\":true, \"children\": [";
				$result .= "{\"title\": \"用户\", \"key\": \"chgpwd\", \"url\": \"/cms/users\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"user.png\" },";
				$result .= "{\"title\": \"角色\", \"key\": \"chgpwd\", \"url\": \"/cms/roles\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"users.png\" },";
				$result .= "{\"title\": \"模板\", \"key\": \"chgpwd\", \"url\": \"/cms/templates\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"tpl.png\"  },";
				$result .= "{\"title\": \"生成\", \"key\": \"chgpwd\", \"url\": \"/cms/genhtml\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"tplgo.png\"  }";
				
				//$result .= "{\"title\": \"数据\", \"key\": \"chgpwd\", \"url\": \"changepwd.aspx\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"database.png\" }";
				$result .= "]}";
				$result .= "]";
			}
			else 
			{
				if(substr($parentID,0,2) == 's_')
				{
					$sql = "SELECT * FROM qf_column where disabled=0 and sid='$parentID' and parentid='' order by sortno ";}
				else
					$sql = "SELECT * FROM qf_column where disabled=0 and parentid='$parentID' order by sortno ";
				
				$query = $this->db->query($sql); 
				foreach ($query->result_array() as $row)
				{
					$result .= ",{ \"title\": \"".$row['cname']."\"";
					$result .= ",\"key\":\"".$row['cid']."\"";
					$result .= ",\"url\":\"/cms/articles/".$row['cid']."\"";
					$result .= ",\"isFolder\":\"true\" ";
					$result .= ",\"icon\":\"col.gif\" ";
					
					if($this->hasChildColumns($row['cid']))
						$result .=",\"isLazy\": \"true\" ";
						
					$result .= "}";
				}
				if(!empty($result))
					$result = "[".substr($result,1)."]";
			}
			return $result;
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return "";
		}
	}
	
	function GetUserSites($uid){
		try{		
			$r = array();
			$sql = "select distinct r.sid from qf_role r, qf_userrole ur where ur.uid='$uid' and r.disabled=0 and ur.rid=r.roleid";
			$query = $this->db->query($sql); 
			//return $query->result_array();
			foreach ($query->result() as $row) {
			 	array_push($r, $row->sid);
			}
			return  $r;
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return null;
		}
	}
	
	function ListUserSites($uid){
		try{		
			$r = array();
			$usersites = $this->GetUserSites($uid);
			
			$sql = "SELECT * FROM qf_site where disabled=0 order by sortno,sid ";
			$query = $this->db->query($sql); 
			foreach ($query->result() as $row){
				//filter
				if($uid != 'admin'){
					if( !in_array($row->sid, $usersites))
						continue;
				}
				
				$r[$row->sid] = $row->sname;
			}
			return  $r;
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return null;
		}
	}
	
	function GetUserName($userid){
		try{
			$result = '';		
			$sql = "SELECT * FROM qf_user WHERE uid ='$userid'";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->uname;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	function ListSiteByUserID($userid){
		try{
			$result = array();
			$sql = "SELECT r.sid FROM qf_userrole ur, qf_role r where ur.uid='$userid' and r.roleid=ur.rid";
			$query = $this->db->query($sql); 
			foreach ($query->result() as $row){
				array_push($result, $row->sid);
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	function ExistUserRole($userid,$roleid){		
		$sql = "SELECT * FROM qf_userrole WHERE uid ='$userid' and rid='$roleid'";
		$query = $this->db->query($sql); 
		if ($query->num_rows()> 0)
			return true;
		else
			return false;		
	}
	
	function GetRoleName($rid){
		try{
			$result = '';		
			$sql = "SELECT * FROM qf_role WHERE roleid ='$rid'";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->rolename;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	//得到用户的权限级别
	function GetRoleLevel($uid){
		try{
			if($uid == 'admin')
				return 3;
				
			$result = 0;		
			$sql = "select max(r.rolelevel) as rolelevel from qf_role r, qf_userrole ur where ur.uid='$uid' and r.disabled=0 and ur.rid=r.roleid;";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->rolelevel;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return 0;
		}
		
	}
	
	function GetSiteIDByColumnID($cid){
		try{
			$result = '';		
			$sql = "SELECT sid FROM qf_column WHERE cid ='$cid'";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->sid;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	function GetPagerHtml($CurPage, $PageCount){
		$output = "<div class='gridPager'>";
		if ($PageCount > 0){
			$output .= "第";
			
			$LinkNum = 10;
			$PageNumX = 1;
			$CurPageX = 1;
			$PageNumX = ceil( $PageCount / $LinkNum );
			$CurPageX = ceil( $CurPage / $LinkNum);

			if ($CurPageX > 1)
				$output.="<a href=\"javascript:goPage(".($CurPageX - 1) * $LinkNum.")\" ><<</a>";
			
			for ($j = ($CurPageX - 1) * $LinkNum + 1; $j <= $PageCount && $j <= $CurPageX * $LinkNum; $j++)
			{
				if ($j == $CurPage)
					$output.="<a href=\"#\" class=\"curPage\">".$j."</a>";
				else
					$output.="<a href=\"javascript:goPage('$j')\" >".$j."</a>";
			}
			if ($CurPageX < $PageNumX)
				$output.="<a href=\"javascript:goPage(".($CurPageX * $LinkNum + 1).")\" >>></a>";

			$output.= "页,共".$PageCount."页";
		}
		$output.= "</div>";
		return $output;
	}
	
	function ListParentIDOfColumn($cid){
		try{
			$pid = '';
			$sql = "SELECT parentid FROM qf_column where cid='$cid'";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$pid = $row->parentid;
			}
			if(!empty($pid)){
				$temp = $this->ListParentIDOfColumn($pid);
				if(!empty($temp))
					$pid = $temp.'/'.$pid;
			}				
			return $pid;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	public function getweekstr(){
		$week = date("w",time());
		$xingqi = '星期';
		switch ($week){
			case 0:
				$xingqi.='日';
				break;
				case 1:
				$xingqi.='一';
				break;
				case 2:
				$xingqi.='二';
				break;
				case 3:
				$xingqi.='三';
				break;
				case 4:
				$xingqi.='四';
				break;
				case 5:
				$xingqi.='五';
				break;
				case 6:
				$xingqi.='六';
				break;
		}
		return $xingqi;
	}
	
	public function gettodayandweekstr(){
		//$today = date('Y年m月d日').' '.$this->getweekstr();
		 #农历每月的天数
		 $everymonth=array(
                    0=>array(8,0,0,0,0,0,0,0,0,0,0,0,29,30,7,1),
                    1=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,8,2),
                    2=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,9,3),
                    3=>array(5,29,30,29,30,29,29,30,29,29,30,30,29,30,10,4),
                    4=>array(0,30,30,29,30,29,29,30,29,29,30,30,29,0,1,5),
                    5=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,2,6),
                    6=>array(4,29,30,30,29,30,29,30,29,30,29,30,29,30,3,7),
                    7=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,4,8),
                    8=>array(0,30,29,29,30,30,29,30,29,30,30,29,30,0,5,9),
                    9=>array(2,29,30,29,29,30,29,30,29,30,30,30,29,30,6,10),
                    10=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,7,11),
                    11=>array(6,30,29,30,29,29,30,29,29,30,30,29,30,30,8,12),
                    12=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,9,1),
                    13=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,10,2),
                    14=>array(5,30,30,29,30,29,30,29,30,29,30,29,29,30,1,3),
                    15=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,2,4),
                    16=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,3,5),
                    17=>array(2,30,29,29,30,29,30,30,29,30,30,29,30,29,4,6),
                    18=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,5,7),
                    19=>array(7,29,30,29,29,30,29,29,30,30,29,30,30,30,6,8),
                    20=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,7,9),
                    21=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,8,10),
                    22=>array(5,30,29,30,30,29,29,30,29,29,30,29,30,30,9,11),
                    23=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,10,12),
                    24=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,1,1),
                    25=>array(4,30,29,30,29,30,30,29,30,30,29,30,29,30,2,2),
                    26=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,3,3),
                    27=>array(0,30,29,29,30,29,30,29,30,29,30,30,30,0,4,4),
                    28=>array(2,29,30,29,29,30,29,29,30,29,30,30,30,30,5,5),
                    29=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,6,6),
                    30=>array(6,29,30,30,29,29,30,29,29,30,29,30,30,29,7,7),
                    31=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,8,8),
                    32=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,9,9),
                    33=>array(5,29,30,30,29,30,30,29,30,29,30,29,29,30,10,10),
                    34=>array(0,29,30,29,30,30,29,30,29,30,30,29,30,0,1,11),
                    35=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,2,12),
                    36=>array(3,30,29,29,30,29,29,30,30,29,30,30,30,29,3,1),
                    37=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,4,2),
                    38=>array(7,30,30,29,29,30,29,29,30,29,30,30,29,30,5,3),
                    39=>array(0,30,30,29,29,30,29,29,30,29,30,29,30,0,6,4),
                    40=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,7,5),
                    41=>array(6,30,30,29,30,30,29,30,29,29,30,29,30,29,8,6),
                    42=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,9,7),
                    43=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,10,8),
                    44=>array(4,30,29,30,29,30,29,30,29,30,30,29,30,30,1,9),
                    45=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,2,10),
                    46=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,3,11),
                    47=>array(2,30,30,29,29,30,29,29,30,29,30,29,30,30,4,12),
                    48=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,5,1),
                    49=>array(7,30,29,30,30,29,30,29,29,30,29,30,29,30,6,2),
                    50=>array(0,29,30,30,29,30,30,29,29,30,29,30,29,0,7,3),
                    51=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,8,4),
                    52=>array(5,29,30,29,30,29,30,29,30,30,29,30,29,30,9,5),
                    53=>array(0,29,30,29,29,30,30,29,30,30,29,30,29,0,10,6),
                    54=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,1,7),
                    55=>array(3,29,30,29,30,29,29,30,29,30,29,30,30,30,2,8),
                    56=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,3,9),
                    57=>array(8,30,29,30,29,30,29,29,30,29,30,29,30,29,4,10),
                    58=>array(0,30,30,30,29,30,29,29,30,29,30,29,30,0,5,11),
                    59=>array(0,29,30,30,29,30,29,30,29,30,29,30,29,0,6,12),
                    60=>array(6,30,29,30,29,30,30,29,30,29,30,29,30,29,7,1),
                    61=>array(0,30,29,30,29,30,29,30,30,29,30,29,30,0,8,2),
                    62=>array(0,29,30,29,29,30,29,30,30,29,30,30,29,0,9,3),
                    63=>array(4,30,29,30,29,29,30,29,30,29,30,30,30,29,10,4),
                    64=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,1,5),
                    65=>array(0,29,30,29,30,29,29,30,29,29,30,30,29,0,2,6),
                    66=>array(3,30,30,30,29,30,29,29,30,29,29,30,30,29,3,7),
                    67=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,4,8),
                    68=>array(7,29,30,29,30,30,29,30,29,30,29,30,29,30,5,9),
                    69=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,6,10),
                    70=>array(0,30,29,29,30,29,30,30,29,30,30,29,30,0,7,11),
                    71=>array(5,29,30,29,29,30,29,30,29,30,30,30,29,30,8,12),
                    72=>array(0,29,30,29,29,30,29,30,29,30,30,29,30,0,9,1),
                    73=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,10,2),
                    74=>array(4,30,30,29,30,29,29,30,29,29,30,30,29,30,1,3),
                    75=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,2,4),
                    76=>array(8,30,30,29,30,29,30,29,30,29,29,30,29,30,3,5),
                    77=>array(0,30,29,30,30,29,30,29,30,29,30,29,29,0,4,6),
                    78=>array(0,30,29,30,30,29,30,30,29,30,29,30,29,0,5,7),
                    79=>array(6,30,29,29,30,29,30,30,29,30,30,29,30,29,6,8),
                    80=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,7,9),
                    81=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,8,10),
                    82=>array(4,30,29,30,29,29,30,29,29,30,29,30,30,30,9,11),
                    83=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,10,12),
                    84=>array(10,30,29,30,30,29,29,30,29,29,30,29,30,30,1,1),
                    85=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,2,2),
                    86=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,3,3),
                    87=>array(6,30,29,30,29,30,30,29,30,30,29,30,29,29,4,4),
                    88=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,5,5),
                    89=>array(0,30,29,29,30,29,29,30,30,29,30,30,30,0,6,6),
                    90=>array(5,29,30,29,29,30,29,29,30,29,30,30,30,30,7,7),
                    91=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,8,8),
                    92=>array(0,29,30,30,29,29,30,29,29,30,29,30,30,0,9,9),
                    93=>array(3,29,30,30,29,30,29,30,29,29,30,29,30,29,10,10),
                    94=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,1,11),
                    95=>array(8,29,30,30,29,30,29,30,30,29,29,30,29,30,2,12),
                    96=>array(0,29,30,29,30,30,29,30,29,30,30,29,29,0,3,1),
                    97=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,4,2),
                    98=>array(5,30,29,29,30,29,29,30,30,29,30,30,29,30,5,3),
                    99=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,6,4),
                    100=>array(0,30,30,29,29,30,29,29,30,29,30,30,29,0,7,5),
                    101=>array(4,30,30,29,30,29,30,29,29,30,29,30,29,30,8,6),
                    102=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,9,7),
                    103=>array(0,30,30,29,30,30,29,30,29,29,30,29,30,0,10,8),
                    104=>array(2,29,30,29,30,30,29,30,29,30,29,30,29,30,1,9),
                    105=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,2,10),
                    106=>array(7,30,29,30,29,30,29,30,29,30,30,29,30,30,3,11),
                    107=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,4,12),
                    108=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,5,1),
                    109=>array(5,30,30,29,29,30,29,29,30,29,30,29,30,30,6,2),
                    110=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,7,3),
                    111=>array(0,30,29,30,30,29,30,29,29,30,29,30,29,0,8,4),
                    112=>array(4,30,29,30,30,29,30,29,30,29,30,29,30,29,9,5),
                    113=>array(0,30,29,30,29,30,30,29,30,29,30,29,30,0,10,6),
                    114=>array(9,29,30,29,30,29,30,29,30,30,29,30,29,30,1,7),
                    115=>array(0,29,30,29,29,30,29,30,30,30,29,30,29,0,2,8),
                    116=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,3,9),
                    117=>array(6,29,30,29,30,29,29,30,29,30,29,30,30,30,4,10),
                    118=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,5,11),
                    119=>array(0,30,29,30,29,30,29,29,30,29,29,30,30,0,6,12),
                    120=>array(4,29,30,30,30,29,30,29,29,30,29,30,29,30,7,1)
                   );
		  #农历天干
		  $mten=array("null","甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
		  #农历地支
		  $mtwelve=array("null","子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)",
		                 "巳(蛇)","午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)");
		  #农历月份
		  $mmonth=array("闰","正","二","三","四","五","六",
		                "七","八","九","十","冬","腊","月");
		  #农历日
		  $mday=array("null","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
		              "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
		              "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十");
		##############################
		  #星期
		  $weekday = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
		
		  #阳历总天数 至1900年12月21日
		  $total=11;
		  #阴历总天数
		  $mtotal=0;
		##############################
		  #获得当日日期
		  $today=getdate();
		  if($today["year"]<1901 || $today["year"]>2020) die("年份出错！");
		
		  $cur_wday=$today["wday"];
		
		  for($y=1901;$y<$today["year"];$y++) { //计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和
		       $total+=365;
		       if ($y%4==0) $total++;
		  }
		
		  switch($today["mon"]) { //再加当年的几个月
		         case 12:
		              $total+=30;
		         case 11:
		              $total+=31;
		         case 10:
		              $total+=30;
		         case 9:
		              $total+=31;
		         case 8:
		              $total+=31;
		         case 7:
		              $total+=30;
		         case 6:
		              $total+=31;
		         case 5:
		              $total+=30;
		         case 4:
		              $total+=31;
		         case 3:
		              $total+=28;
		         case 2:
		              $total+=31;
		  }
		
		  if($today["year"]%4 == 0 && $today["mon"]>2) $total++; //如果当年是闰年还要加一天
		
		  $total=$total+$today["mday"]-1; //加当月的天数
		
		  $flag1=0;  //判断跳出循环的条件
		  $j=0;
		  while ($j<=120){  //用农历的天数累加来判断是否超过阳历的天数
		      $i=1;
		      while ($i<=13){
		            $mtotal+=$everymonth[$j][$i];
		            if ($mtotal>=$total){
		                 $flag1=1;
		                 break;
		            }
		            $i++;
		      }
		      if ($flag1==1) break;
		      $j++;
		  }
		
		  if($everymonth[$j][0]<>0 and $everymonth[$j][0]<$i){ //原来错在这里，对闰月没有修补
		      $mm=$i-1;
		  }
		  else{
		      $mm=$i;
		  }
		
		  if($i==$everymonth[$j][0]+1 and $everymonth[$j][0]<>0) {
		      $nlmon=$mmonth[0].$mmonth[$mm];#闰月
		  }
		  else {
		      $nlmon=$mmonth[$mm].$mmonth[13];
		  }
		
		  #计算所求月份1号的农历日期
		  $md=$everymonth[$j][$i]-($mtotal-$total);
		  if($md > $everymonth[$j][$i])
		      $md-=$everymonth[$j][$i];
		  $nlday=$mday[$md];
		
		  $nowday = date("Y年n月j日  ").$weekday[$cur_wday]."&nbsp;&nbsp;&nbsp;&nbsp;".$mten[$everymonth[$j] [14]].$mtwelve[$everymonth[$j][15]]."年&nbsp;".$nlmon.$nlday;
		  //echo "<font color=#3333ff>$nowday</font>";
		  return $nowday;
		
	}
	
	public function findtemplate($id, $flag=0){
		$result = array();
		
		if(empty($id))
			return null;
			
		$prefix = substr($id,0,2);
		
		if($prefix === 's_'){
			$sid = $id;
			$tid = '';
			$content = '';
			$sql = "select templateid from qf_site where sid='$sid' and disabled=0 and sitestatus=0 ";
			$query = $this->db->query($sql);
			if($query->num_rows() > 0){
				$row = $query->row();
				$tid = $row->templateid;
			}
			if(empty($tid))
				$tid = 't_001';		
			$sql = "select tcontent as content from qf_template where tid='$tid'";
			if(!empty($flag))
				$sql = "select scontent as content from qf_template where tid='$tid'";
			$querytpl = $this->db->query($sql);
			if($querytpl->num_rows() > 0){
				$row = $querytpl->row();
				$content = $row->content;
			}			
			$result = array('sid'=>$sid, 'tid'=>$tid, 'content'=>$content);
		}
		else if($prefix === 'c_'){
			$sid = '';
			$cid = $id;
			$tid = '';
			$content = '';
			$sql = "select * from qf_column where cid='$cid'";
			$querycol = $this->db->query($sql);
			if($querycol->num_rows() > 0){
				$row = $querycol->row();
				$tid = $row->templateid;
				$sid = $row->sid;
			}
			if(empty($tid))
				$tid='t_002';
			$sql = "select tid, tcontent as content from qf_template where tid='$tid'";
			if(!empty($flag))
				$sql = "select tid, scontent as content from qf_template where tid='$tid'";
			$querytpl = $this->db->query($sql);
			if($querytpl->num_rows() > 0){
				$row = $querytpl->row();
				$content = $row->content;
			}
			$result = array('sid'=>$sid, 'tid'=>$tid, 'content'=>$content);
		}
		else if($prefix === 'a_'){
			$sid = '';
			$aid = $id;
			$tid = '';
			$content = '';
			$sql = "select * from qf_article where aid='$aid'";
			$querycol = $this->db->query($sql);
			if($querycol->num_rows() > 0){
				$row = $querycol->row();
				$tid = $row->templateid;
				$sid = $row->sid;
			}
			if(empty($tid))
				$tid='t_003';
			$sql = "select tid, tcontent as content from qf_template where tid='$tid'";
			if(!empty($flag))
				$sql = "select tid, scontent as content from qf_template where tid='$tid'";
			$querytpl = $this->db->query($sql);
			if($querytpl->num_rows() > 0){
				$row = $querytpl->row();
				$content = $row->content;
			}
			$result = array('sid'=>$sid, 'tid'=>$tid, 'content'=>$content);
		}
		
		return $result;
	}
	
	public function getcolname($cid){
		$colname = '';
		$this->db->where('cid',$cid);
		$query =$this->db->get('qf_column');
		if($query->num_rows() > 0){
			$row = $query->row();
			$colname = $row->cname;
		}
		return $colname;
	}
	
	public function listmenucols($sid, $tag, $flag=0){
		$result = array();
		
		$sql = "select * from qf_column where parentid='' and sid='$sid' and disabled='0' and display='0' ";
		if(!empty($tag))
			$sql.= "and tag='$tag' ";
		$sql.= "order by sortno asc";
		$menucols = $this->db->query($sql);		
		foreach ($menucols->result() as $row){
			$url = '';
			if(empty($flag))
				$url = "/view/col/".$row->cid;
			else{
				$temp = $this->ListParentIDOfColumn($row->cid);
				if(empty($temp))
					$url = "/".$row->cid;
				else 
					$url = "/".$temp."/".$row->cid;
			}
			$result[$url] = $row->cname;			
		}
		return $result;
	}
	
	public function getnavcols($cid, $flag=0){
		$result = array();
		$pidstr = $this->ListParentIDOfColumn($cid);
		if(empty($pidstr))
			return null;
		
		$parray = explode('/', $pidstr);
		foreach ($parray as $pid){			
			$pname = $this->getcolname($pid);
			$url = '';
			if(empty($flag))
				$url = "/view/col/".$pid;
			else{
				$temp = $this->ListParentIDOfColumn($pid);
				if(empty($temp))
					$url = "/".$pid;
				else 
					$url = "/".$temp."/".$pid;
			}
			$result[$url] = $pname;
		}
		return $result;
	}
	
	public function listcolarticles($cid,$pagenum, $pagesize, $flag=0){
		/**{section name=sec_art loop=$articles}
              {if $articles[sec_art].atype == "0"}
                <p><a href="/view/art/{$articles[sec_art].aid}">{$articles[sec_art].title}</a></p>
{elseif $articles[sec_art].atype == "1" }
 <p><a href="{$articles[sec_art].atturl}" target="_blank">{$articles[sec_art].title}(附件)</a></p>
{else}
<p><a href="{$articles[sec_art].linkurl}" target="_blank">{$articles[sec_art].title}(超链)</a></p>
{/if}
          {/section}
          */
		$result = array();
		
		if(empty($pagenum))
				$pagenum = 1;	
		$start = $pagesize * ($pagenum-1);
		
		$sql = "select * from qf_article where cid='$cid' and disabled='0' and display='0' and astatus='1' order by sortno asc, updatetime desc";
		$sql .=" limit ".$start.",".$pagesize;
		$queryarts = $this->db->query($sql);
		foreach ($queryarts->result() as $row){
			$url = '';
			$atitle = $row->title;
			switch ($row->atype){
				case '0':
					if(empty($flag))
						$url = "/view/art/".$row->aid;
					else{
						$temp = $this->ListParentIDOfColumn($row->cid);
						if(!empty($temp))
							$url = "/".$temp."/".$row->cid."/".$row->aid.".html";
						else 
							$url = "/".$row->cid."/".$row->aid.".html";
					}
					break;
				case '1':
					$url = $row->atturl;
					$atitle.='(附件)';
					break;
				case '2':
					$url = $row->linkurl;
					$atitle.='(超链接)';
					break;
			}
			$result[$url] = $atitle;
		}
		return $result;
	}
	
	public function listcolarticlesbycname($cname, $pagenum, $pagesize, $flag=0){
		$cid='';
		$sql = "SELECT * FROM qf_column where cname='$cname' and disabled=0 and parentid=''";
		$query = $this->db->query($sql);
		if($query->num_rows() > 0){
			$row = $query->row();
			$cid = $row->cid;
		}
		
		$result = array();
		$temp = $this->listcolarticles($cid, $pagenum, $pagesize, $flag);
		foreach ($temp as $key=>$value){
			$svalue = $value;
			if($this->strlen_utf8($value) > 15)
				$svalue = $this->cut_str($value,15)."...";
			/*if( strlen($value) > 15 )
				$svalue = substr($value, 0, 15);*/
				
			$result[$key] = $svalue;
		}
		
		return $result;
	}
	
	function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
	{ 
		if($code == 'UTF-8') 
		{ 
			$pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
			preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)); 
			return join('', array_slice($t_string[0], $start, $sublen)); 
		} 
		else 
		{ 
			$start = $start*2; 
			$sublen = $sublen*2; 
			$strlen = strlen($string); 
			$tmpstr = ''; 
			for($i=0; $i<$strlen; $i++) 
			{ 
				if($i>=$start && $i<($start+$sublen)) 
				{ 
					if(ord(substr($string, $i, 1))>129) 
					{ 
						$tmpstr.= substr($string, $i, 2); 
					} 
					else 
					{ 
						$tmpstr.= substr($string, $i, 1); 
					} 
				} 
				if(ord(substr($string, $i, 1))>129) $i++; 
			} 
			if(strlen($tmpstr)<$strlen ) $tmpstr.= ""; 
			return $tmpstr; 
		} 
	} 
	
	function strlen_utf8($str) {
		$i = 0;
		$count = 0;
		$len = strlen ($str);
		while ($i < $len) {
			$chr = ord ($str[$i]);
			$count++;
			$i++;
			if($i >= $len) break;
			if($chr & 0x80) {
				$chr <<= 1;
				while ($chr & 0x80) {
				$i++;
				$chr <<= 1;
				}
			}
		}
		return $count;
	}
	
	public function listsubcolumns($cid, $flag=0){
		$result = array();
		
		$sql = "SELECT * FROM qf_column where disabled=0 and parentid='$cid' order by sortno";
		$querysubcol = $this->db->query($sql);	
		foreach ($querysubcol->result() as $row){
			$url = '';
			if(empty($flag))
				$url = "/view/col/".$row->cid;
			else{
				$temp = $this->ListParentIDOfColumn($row->cid);
				if(empty($temp))
					$url = "/".$row->cid;
				else 
					$url = "/".$temp."/".$row->cid;
			}
			$result[$url] = $row->cname;			
		}
		return $result;
	}
	
	public function listsubcolumnsbycname($cname, $flag=0){
		$result = array();
		
		$cid='';
		$sql = "SELECT * FROM qf_column where cname='$cname' and disabled=0 and parentid=''";
		$query = $this->db->query($sql);
		if($query->num_rows() > 0){
			$row = $query->row();
			$cid = $row->cid;
		}	
		
		$sql = "SELECT * FROM qf_column where disabled=0 and parentid='$cid' order by sortno";
		$querysubcol = $this->db->query($sql);	
		foreach ($querysubcol->result() as $row){
			$url = '';
			if(empty($flag))
				$url = "/view/col/".$row->cid;
			else{
				$temp = $this->ListParentIDOfColumn($row->cid);
				if(empty($temp))
					$url = "/".$row->cid;
				else 
					$url = "/".$temp."/".$row->cid;
			}
			$result[$url] = $row->cname;			
		}
		return $result;
	}
	
	public function getcollinkbyname($cname, $flag=0){
		//$result = array();
		$result = '';
		
		$sql = "SELECT * FROM qf_column where cname='$cname' and disabled=0 and parentid=''";
		$query = $this->db->query($sql);	
		foreach ($query->result() as $row){
			$url = '';
			if(empty($flag))
				$url = "/view/col/".$row->cid;
			else{
				$temp = $this->ListParentIDOfColumn($row->cid);
				if(empty($temp))
					$url = "/".$row->cid;
				else 
					$url = "/".$temp."/".$row->cid;
			}
			//$result[$url] = $row->cname;		
			$result = $url;	
		}
		return $result;
	}
	
	public function getpicnews($flag=0){
		/**imgUrl1="images/1.jpg";
			imgtext1="图片新闻1"
			imgLink1=escape("http://");
			imgUrl2="images/2.jpg";
			imgtext2="图片新闻2"
			imgLink2=escape("http://");
			imgUrl3="images/1.jpg";
			imgtext3="图片新闻3"
			imgLink3=escape("http://");
			imgUrl4="images/2.jpg";
			imgtext4="图片新闻4"
			imgLink4=escape("http://");
			imgUrl5="images/1.jpg";
			imgtext5="图片新闻5"
			imgLink5=escape("http://");
			*/
		$result = '';
		
		$cid = '';
		$sql = "SELECT * FROM qf_column where cname='图片新闻' and disabled=0 and parentid=''";
		$query = $this->db->query($sql);
		if($query->num_rows() > 0){
			$row = $query->row();
			$cid = $row->cid;
		}	
		$sql = "select * from qf_article where cid='$cid' and disabled=0 and display=0 and astatus=1 order by sortno, updatetime desc limit 5";
		$i = 0;
		$query = $this->db->query($sql);
		foreach ($query->result() as $row){
			$i++;
			
			$result .= "imgUrl".$i."='$row->titlethumbs';";
			$result .= "imgtext".$i."='$row->title';";
			if(empty($flag))
				$result .= "imgLink".$i."='/view/art/".$row->aid."';";
			else {
				$url = '';
				$temp = $this->ListParentIDOfColumn($row->cid);
				if(!empty($temp))
					$url = "/".$temp."/".$row->cid."/".$row->aid.".html";
				else 
					$url = "/".$row->cid."/".$row->aid.".html";
				$result .= "imgLink".$i."='$url';";
			}
		}
		return $result;
	}
	
	public function getleaders($flag=0){
		$cid = '';
		$sql = "SELECT * FROM qf_column where cname='领导简介' and disabled=0 and parentid=''";
		$query = $this->db->query($sql);
		if($query->num_rows() > 0){
			$row = $query->row();
			$cid = $row->cid;
		}	
		
		$sql = "SELECT * FROM qf_column where disabled=0 and parentid='$cid'";
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	
}